package com.fc.test.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.fc.test.mapper.auto.TAbbRenzhengMapper;
import com.fc.test.model.auto.*;
import com.fc.test.util.OrderNOUtil;
import com.fc.test.util.StringUtils;
import com.fc.test.util.wx.WxXcxPayService;
import org.apache.shiro.SecurityUtils;
import java.util.Map;

import com.fc.test.mapper.auto.*;
import com.fc.test.model.auto.*;
import com.fc.test.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.Convert;
import com.fc.test.mapper.auto.TAbbOrderMapper;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service
* @Title: TAbbOrderService.java 
* @Package com.fc.test.entity 
* @author fuce
* @email 87766867@qq.com
* @date 2019-08-21 16:15:03  
 */
@Service
public class TAbbOrderService implements BaseService<TAbbOrder, TAbbOrderExample>{
	@Autowired
	private TAbbOrderMapper tAbbOrderMapper;
	@Autowired
	private TAbbRenzhengMapper tAbbRenzhengMapper;

	@Autowired
	private TAbbMemberMapper tAbbMemberMapper;

	@Autowired
	private TAbbGoodsMapper tAbbGoodsMapper;

	@Autowired
	private TAbbPinglunMapper tAbbPinglunMapper;

	@Autowired
	private TAbbTousuMapper tAbbTousuMapper;

	@Autowired
	private TAbbTaocanMapper tAbbTaocanMapper;

	@Autowired
	private TAbbQuanchengMapper quanchengMapper;

	@Autowired
	private TAbbBiamminMapper biamminMapper;

	@Autowired
	private TAbbQuanminMapper quanminMapper;

	@Autowired
	private TAbbQczifeiMapper qczifeiMapper;

	@Autowired
	private TAbbCountMapper countMapper;

	@Autowired
	private TAbbCommissionMapper commissionMapper;

	@Autowired
	private TAbbTgzifeiMapper tAbbTgzifeiMapper;
	@Autowired
	private AbbPingtaiMapper abbPingtaiMapper;

	@Autowired
	private TAbbCxzifeiMapper cxzifeiMapper;

	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<Map> list(Tablepar tablepar,String name){
	       /* TAbbOrderExample testExample=new TAbbOrderExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
	        	testExample.createCriteria().andOrdernoLike("%"+name+"%");
	        }*/
	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        //List<TAbbOrder> list= tAbbOrderMapper.selectByExample(testExample);
	    	 List<Map> list =tAbbOrderMapper.selectByExample1(name);
	        PageInfo<Map> pageInfo = new PageInfo<Map>(list);
	        return  pageInfo;
	 }
	/**
	 * 分页查询
	 * @param
	 * @param pageSize
	 * @param searchTxt
	 * @param starttime
	 * @return
	 */
	public PageInfo<Map> list1(Tablepar tablepar, String name,String starttime,String endtime){
	       /* TAbbOrderExample testExample=new TAbbOrderExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
	        	testExample.createCriteria().andOrdernoLike("%"+name+"%");
	        }*/
		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		//List<TAbbOrder> list= tAbbOrderMapper.selectByExample(testExample);
		TsysUser user= (TsysUser) SecurityUtils.getSubject().getPrincipal();
		TAbbRenzhengExample testExample=new TAbbRenzhengExample();
		testExample.createCriteria().andLoginusernameEqualTo(user.getUsername());
		List<TAbbRenzheng> list1= tAbbRenzhengMapper.selectByExample(testExample);
		String shopid=list1.get(0).getId();
		List<Map> list =tAbbOrderMapper.selectByExample2(name,shopid,starttime,endtime);
		PageInfo<Map> pageInfo = new PageInfo<Map>(list);
		return  pageInfo;
	}
	/**
	 * 分页查询
	 * @param
	 * @param pageSize
	 * @param starttime
     * @param endtime
     * @return
	 */
	public PageInfo<TAbbOrder> list2(Tablepar tablepar, String name, Integer paystatus, String starttime, String endtime){
	      TAbbOrderExample testExample=new TAbbOrderExample();
	        testExample.setOrderByClause("createtime DESC");
	    	TAbbOrderExample.Criteria criteria=testExample.createCriteria();
	    	if (paystatus==null){
				criteria.andPaystatusEqualTo(1);
			}else{
				if (paystatus==5){
					criteria.andPaystatusGreaterThan(2);
				}else{
					if (paystatus!=6){
						criteria.andPaystatusEqualTo(paystatus);
					}
				}
			}
	        if(name!=null&&!"".equals(name)){
				criteria.andYuliu4Like("%"+name+"%");
	        }
		if(starttime!=null&&!"".equals(starttime)){
			criteria.andCreatetimeGreaterThanOrEqualTo(starttime);
		}
		if(endtime!=null&&!"".equals(endtime)){
			criteria.andCreatetimeLessThanOrEqualTo(endtime);
		}
		   criteria.andTypeEqualTo(3);

		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		List<TAbbOrder> list= tAbbOrderMapper.selectByExample(testExample);
		PageInfo<TAbbOrder> pageInfo = new PageInfo<TAbbOrder>(list);
		return  pageInfo;
	}
	@Override
	public int deleteByPrimaryKey(String ids) {
		List<String> lista=Convert.toListStrArray(ids);
		TAbbOrderExample example=new TAbbOrderExample();
		example.createCriteria().andIdIn(lista);
		return tAbbOrderMapper.deleteByExample(example);
	}


	@Override
	public TAbbOrder selectByPrimaryKey(String id) {

		return tAbbOrderMapper.selectByPrimaryKey(id);
	}


	@Override
	public int updateByPrimaryKeySelective(TAbbOrder record) {
		record.setYuliu9(DateUtil.getCurrentDateTimeStr());
		record.setPaystatus(2);
		return tAbbOrderMapper.updateByPrimaryKeySelective(record);
	}

	@Transactional
	public int xiaofei(TAbbOrder record) {
		//后台佣金计算
		TAbbOrder record1=tAbbOrderMapper.selectByPrimaryKey(record.getId());//购买的订单实体
		TAbbMember tAbbMember1=tAbbMemberMapper.selectByPrimaryKey(record1.getUserid());//购买订单的用户实体
		TAbbGoods tAbbGoods=tAbbGoodsMapper.selectByPrimaryKey(record1.getObjid());//购买的商品实体

		TAbbRenzheng tAbbRenzheng=tAbbRenzhengMapper.selectByPrimaryKey(record1.getShopid());//购买商品的商家表实体
		TAbbMember shangjiatAbbMember=tAbbMemberMapper.selectByPrimaryKey(tAbbRenzheng.getUserid());//购买商品的商家用户实体

		TAbbMember shangjitAbbMember=new TAbbMember();//购买商品的上级用户实体
		//算出后台和上级的佣金
		AbbPingtaiExample example=new AbbPingtaiExample();
		List<AbbPingtai> listabbPingtai= abbPingtaiMapper.selectByExample(example);
		String pingtai=listabbPingtai.get(0).getPingtai();//平台佣金比例
		String shangji=listabbPingtai.get(0).getShangji();//上级佣金比例
		double pingtaiCommission=record1.getPaymoney()*Double.parseDouble(pingtai)/100;//平台佣金

		//判断是否有上级用户
		double shangjiCommission=0;
		if (tAbbMember1.getYqrid()!=null){
			shangjitAbbMember=tAbbMemberMapper.selectByPrimaryKey(tAbbMember1.getYqrid());
			if (shangjitAbbMember!=null && shangjitAbbMember.getIsvip()==1){
				shangjiCommission=record1.getPaymoney()*Double.parseDouble(shangji)/100;//上级佣金
			}else{
				pingtaiCommission=pingtaiCommission+record1.getPaymoney()*Double.parseDouble(shangji)/100; //把上级佣金给平台
			}
		}else{
			pingtaiCommission=pingtaiCommission+record1.getPaymoney()*Double.parseDouble(shangji)/100; //把上级佣金给平台
		}
		double shangjiaPrice=record1.getPaymoney()-pingtaiCommission-shangjiCommission;//商家所得金额

		//增加商户账户的钱
		TAbbMember tAbbMember=new TAbbMember();
		tAbbMember.setAccount(shangjiatAbbMember.getAccount()+shangjiaPrice);
		tAbbMember.setId(shangjiatAbbMember.getId());
		tAbbMemberMapper.updateByPrimaryKeySelective(tAbbMember);

		//增加平台账户的钱
		TAbbCountExample example1=new TAbbCountExample();
		List<TAbbCount> listcount=countMapper.selectByExample(example1);
		TAbbCount tAbbCount=listcount.get(0);
		tAbbCount.setCount(tAbbCount.getCount()+pingtaiCommission);
		countMapper.updateByPrimaryKeySelective(tAbbCount);

		//增加上级用户账户的钱
        if (shangjiCommission!=0){
			TAbbMember tAbbMember3=new TAbbMember();
			tAbbMember3.setId(shangjitAbbMember.getId());
			if (shangjitAbbMember!=null && shangjitAbbMember.getId().equals(shangjiatAbbMember.getId()) && shangjitAbbMember.getIsvip()==1){
				tAbbMember3.setAccount(shangjitAbbMember.getAccount()+shangjiCommission+shangjiaPrice);
			}else{
				tAbbMember3.setAccount(shangjitAbbMember.getAccount()+shangjiCommission);
			}
			tAbbMemberMapper.updateByPrimaryKeySelective(tAbbMember3);
		}

		//添加商家佣金记录表
		TAbbCommission tAbbCommission1=new TAbbCommission();
		tAbbCommission1.setCreattime(DateUtil.getCurrentDateTimeStr());
		tAbbCommission1.setType(2);
		tAbbCommission1.setBeiyongone(String.valueOf(shangjiaPrice));
		tAbbCommission1.setBusinessid("2");
		tAbbCommission1.setMemberid(shangjiatAbbMember.getId());
		tAbbCommission1.setMessage("商品收益");//tAbbMember1.getNicename()+"购买了:"+tAbbGoods.getGoodsname()+"消费了"+shangjiaPrice+"元!"
        tAbbCommission1.setId(SnowflakeIdWorker.getUUID());
        commissionMapper.insertSelective(tAbbCommission1);

		//添加平台佣金记录表
		TAbbCommission tAbbCommission2=new TAbbCommission();
        tAbbCommission2.setCreattime(DateUtil.getCurrentDateTimeStr());
        tAbbCommission2.setType(3);
        tAbbCommission2.setBeiyongone(String.valueOf(pingtaiCommission));
        tAbbCommission2.setBusinessid("1");
        tAbbCommission2.setMemberid(record1.getUserid());
        tAbbCommission2.setMessage(tAbbMember1.getNicename()+"购买了:"+tAbbGoods.getGoodsname()+"系统获得了"+pingtaiCommission+"元提成!");
        tAbbCommission2.setId(SnowflakeIdWorker.getUUID());
        commissionMapper.insertSelective(tAbbCommission2);

		//添加上级用户佣金记录表
        if (shangjiCommission!=0){
            TAbbCommission tAbbCommission3=new TAbbCommission();
            tAbbCommission3.setCreattime(DateUtil.getCurrentDateTimeStr());
            tAbbCommission3.setType(1);
            tAbbCommission3.setBeiyongone(String.valueOf(shangjiCommission));
            tAbbCommission3.setBusinessid("1");
            tAbbCommission3.setMemberid(shangjitAbbMember.getId());
            tAbbCommission3.setMessage("下级消费抽成!");
            tAbbCommission3.setId(SnowflakeIdWorker.getUUID());
            commissionMapper.insertSelective(tAbbCommission3);
        }
		return tAbbOrderMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 添加
	 */
	@Override
	public int insertSelective(TAbbOrder record) {
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
		return tAbbOrderMapper.insertSelective(record);
	}










	@Override
	public int updateByExampleSelective(TAbbOrder record, TAbbOrderExample example) {

		return tAbbOrderMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(TAbbOrder record, TAbbOrderExample example) {

		return tAbbOrderMapper.updateByExample(record, example);
	}

	@Override
	public List<TAbbOrder> selectByExample(TAbbOrderExample example) {

		return tAbbOrderMapper.selectByExample(example);
	}


	@Override
	public long countByExample(TAbbOrderExample example) {

		return tAbbOrderMapper.countByExample(example);
	}


	@Override
	public int deleteByExample(TAbbOrderExample example) {

		return tAbbOrderMapper.deleteByExample(example);
	}


	public Map<String,Object> createCuxiaoOrder(String userid,String goodsid,int count,int type,String receiverName, String receiverPhone,String address){
		Map<String,Object> resMap = new HashMap<>();

		TAbbMember tAbbMember = tAbbMemberMapper.selectByPrimaryKey(userid);
		TAbbGoods tAbbGoods = tAbbGoodsMapper.selectByPrimaryKey(goodsid);
		DecimalFormat df = new DecimalFormat("#.00");

		TAbbOrder order = new TAbbOrder();
		double goodsmoney = 0d;
		if(1==type){
			order.setRemark("确认消费");
			goodsmoney = Double.parseDouble(df.format(tAbbGoods.getGoodsbaoyouprice()*count));
		}else if(2==type){
			order.setRemark("到店消费");
			goodsmoney = Double.parseDouble(df.format(tAbbGoods.getGoodszitiprice()*count));
		}/*else if(3==type){
			order.setRemark("邮寄");
			goodsmoney = Double.parseDouble(df.format(tAbbGoods.getGoodsyunsongprice()*count));
		}*/
		order.setId(SnowflakeIdWorker.getUUID());
		order.setCreatetime(DateUtil.getCurrentDateTimeStr());
		order.setType(3);
		order.setOrderno(OrderNOUtil.getOrderNoByTime());
		order.setGoodsmoney(goodsmoney);
		order.setPaymoney(goodsmoney);
		order.setPaystatus(0);
		order.setUserid(userid);
		order.setObjid(goodsid);
		order.setShopid(tAbbGoods.getShopid());
		order.setYuliu3(tAbbGoods.getGoodsname());
		order.setYuliu4(tAbbMember.getNicename());
		order.setYuliu5(String.valueOf(count));
		order.setTransactionid(address);
		order.setYuliu7(receiverPhone);
		order.setYuliu8(receiverName);

		int flag = tAbbOrderMapper.insert(order);
		if(flag>0){
			resMap.put("success",true);
			resMap.put("msg","生成促销订单成功");
			if(order.getPaymoney()>0){
				resMap.put("orderid",order.getId());
			}else{
				resMap.put("orderid","");
			}
		}else{
			resMap.put("success",false);
			resMap.put("msg","生成促销订单失败");
		}
		return resMap;
	}


	public Map<String,Object> createHuigouOrder(String userid,String goodsid,int count){
		Map<String,Object> resMap = new HashMap<>();
		TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(userid);
		TAbbGoods goods = tAbbGoodsMapper.selectByPrimaryKey(goodsid);
		DecimalFormat df = new DecimalFormat("#.00");
		double goodsmoney = Double.parseDouble(df.format(goods.getGoodsyunsongprice()*count));
		TAbbOrder order = new TAbbOrder();
		order.setId(SnowflakeIdWorker.getUUID());
		order.setCreatetime(DateUtil.getCurrentDateTimeStr());
		order.setType(5);
		order.setOrderno(OrderNOUtil.getOrderNoByTime());
		order.setGoodsmoney(goodsmoney);
		order.setPaymoney(goodsmoney);
		order.setPaystatus(0);
		order.setUserid(userid);
		order.setObjid(goodsid);
		order.setShopid(goods.getShopid());
		order.setYuliu3(goods.getGoodsname());
		order.setYuliu4(member.getNicename());
		order.setYuliu5(String.valueOf(count));

		int flag = tAbbOrderMapper.insert(order);
		if(flag>0){
			resMap.put("success",true);
			resMap.put("msg","生成大商惠购订单成功");
			if(order.getPaymoney()>0){
				resMap.put("orderid",order.getId());
			}else{
				resMap.put("orderid","");
			}
		}else{
			resMap.put("success",false);
			resMap.put("msg","生成大商惠购订单失败");
		}
		return resMap;
	}


	public Map<String,Object> getMyCuxiaoOrder(String userid,int paystatus,int page){
		Map<String,Object> resMap = new HashMap<>();
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("userid",userid);
		paramMap.put("paystatus",paystatus);
		paramMap.put("page",page);
		List<TAbbOrder> list = tAbbOrderMapper.getMyCuxiaoOrder(paramMap);
		if(list.size()>0){
			resMap.put("msg","获取我的促销订单成功");

		}else{
			resMap.put("msg","到底了");
		}
		resMap.put("success",true);
		resMap.put("list",list);
		return resMap;
	}

	public Map<String,Object> getMyHuigouOrder(String userid,int page){
		Map<String,Object> resMap = new HashMap<>();
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("userid",userid);
		paramMap.put("page",page);
		List<TAbbOrder> list = tAbbOrderMapper.getMyHuigouOrder(paramMap);
		if(list.size()>0){
			resMap.put("msg","获取我的惠购订单成功");
		}else{
			resMap.put("msg","到底了");
		}
		resMap.put("success",true);
		resMap.put("list",list);
		return resMap;
	}

	@Transactional
	public Map<String,Object> confirmReceipt(String orderid){
		Map<String,Object> resMap = new HashMap<>();
		TAbbOrder order = tAbbOrderMapper.selectByPrimaryKey(orderid);
		order.setPaystatus(3);
		int flag = tAbbOrderMapper.updateByPrimaryKey(order);

		/*TAbbRenzheng shop = tAbbRenzhengMapper.selectByPrimaryKey(order.getShopid());
		TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(shop.getUserid());
		member.setAccount(member.getAccount()+order.getPaymoney());
		tAbbMemberMapper.updateByPrimaryKey(member);*/

		//增加一条平台的收益记录
		/*TAbbCommission commission = new TAbbCommission();
		commission.setId(SnowflakeIdWorker.getUUID());
		commission.setCreattime(DateUtil.getCurrentDateTimeStr());
		commission.setBusinessid("2");
		commission.setType(3);
		commission.setMessage("商品收益");
		commission.setBeiyongone(String.valueOf(order.getPaymoney()));
		commission.setBeiyongtwo(order.getObjid());
		commissionMapper.insert(commission);*/

		if(flag>0){
			resMap.put("success",true);
			resMap.put("msg","操作成功");
		}else{
			resMap.put("success",false);
			resMap.put("msg","操作失败，请重试");
		}
		return resMap;
	}


	@Transactional
	public Map<String,Object> cancelHuigouOrder(String orderid){
		Map<String,Object> resMap = new HashMap<>();
		TAbbOrder order = tAbbOrderMapper.selectByPrimaryKey(orderid);
		TAbbRenzheng renzheng = tAbbRenzhengMapper.selectByPrimaryKey(order.getShopid());
		TAbbMember shop = tAbbMemberMapper.selectByPrimaryKey(renzheng.getUserid());
		TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(order.getUserid());
		DecimalFormat df = new DecimalFormat("#.00");

		//订单改为取消
		order.setYuliu1("3");
		int flag = tAbbOrderMapper.updateByPrimaryKey(order);

		//从商家钱包减去订单金额
		double account = shop.getAccount();
		account -= order.getPaymoney();
		shop.setAccount(Double.parseDouble(df.format(account)));
		tAbbMemberMapper.updateByPrimaryKey(shop);

		//用户钱包加上订单金额
		double memberAccount = member.getAccount();
		memberAccount += order.getPaymoney();
		member.setAccount(Double.parseDouble(df.format(memberAccount)));
		tAbbMemberMapper.updateByPrimaryKey(member);

		//增加一条买家的退款记录
		TAbbCommission commission = new TAbbCommission();
		commission.setId(SnowflakeIdWorker.getUUID());
		commission.setCreattime(DateUtil.getCurrentDateTimeStr());
		commission.setMemberid(order.getUserid());
		commission.setBusinessid("1");
		commission.setType(1);
		commission.setMessage("惠购退款");
		commission.setBeiyongone(String.valueOf(order.getPaymoney()));
		commission.setBeiyongtwo(order.getObjid());
		commissionMapper.insert(commission);

		if(flag>0){
			resMap.put("success",true);
			resMap.put("msg","取消成功");
		}else{
			resMap.put("success",false);
			resMap.put("msg","取消失败");
		}
		return resMap;
	}

	@Transactional
	public int huigouPinglun(String orderid,String content){
		TAbbOrder order = tAbbOrderMapper.selectByPrimaryKey(orderid);

		TAbbPinglun pinglun = new TAbbPinglun();
		pinglun.setId(SnowflakeIdWorker.getUUID());
		pinglun.setCreattime(DateUtil.getCurrentDateTimeStr());
		pinglun.setContent(content);
		pinglun.setUserid(order.getUserid());
		pinglun.setGoodsid(order.getObjid());
		pinglun.setUsername(order.getYuliu4());
		int flag = tAbbPinglunMapper.insert(pinglun);

		//修改订单状态
		order.setPaystatus(3);
		tAbbOrderMapper.updateByPrimaryKey(order);

		return flag;
	}

	public int huigouTousu(String orderid,String content){
		TAbbOrder order = tAbbOrderMapper.selectByPrimaryKey(orderid);

		TAbbTousu tousu = new TAbbTousu();
		tousu.setId(SnowflakeIdWorker.getUUID());
		tousu.setCreattime(DateUtil.getCurrentDateTimeStr());
		tousu.setContent(content);
		tousu.setGoodsid(order.getObjid());
		tousu.setUserid(order.getUserid());
		tousu.setUsername(order.getYuliu4());
		int flag = tAbbTousuMapper.insert(tousu);

		//修改订单状态
		order.setPaystatus(4);
		tAbbOrderMapper.updateByPrimaryKey(order);

		return flag;
	}

	public Map<String,Object> createVipOrder(String userid,String taocanid){
		Map<String,Object> resMap = new HashMap<>();
		TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(userid);
		TAbbTaocan taocan = tAbbTaocanMapper.selectByPrimaryKey(taocanid);

		TAbbOrder order = new TAbbOrder();
		order.setId(SnowflakeIdWorker.getUUID());
		order.setCreatetime(DateUtil.getCurrentDateTimeStr());
		order.setType(6);
		order.setOrderno(OrderNOUtil.getOrderNoByTime());
		order.setGoodsmoney(taocan.getMoney());
		order.setPaymoney(taocan.getMoney());
		order.setPaystatus(0);
		order.setUserid(userid);
		order.setObjid(taocanid);
		order.setYuliu3("购买vip");
		order.setYuliu4(member.getNicename());
		int flag = tAbbOrderMapper.insert(order);
		if(flag>0){
			resMap.put("success",true);
			resMap.put("msg","创建vip订单成功");
			resMap.put("orderid",order.getId());
		}else{
			resMap.put("success",false);
			resMap.put("msg","创建vip订单失败");
		}
		return resMap;
	}


	public Map<String, Object> createWxOrder(String orderId, String ip) {
		String body = "";
		String detail = "";
		TAbbOrder order = tAbbOrderMapper.selectByPrimaryKey(orderId);
		TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(order.getUserid());
		String openId = member.getOpenid();
		//订单号
		String orderNo = order.getOrderno();
		//支付金额
		String money = String.valueOf(order.getPaymoney());
		int type = order.getType();
		if(1==type){
            body = "发布全城任务";
            detail = "发布全城任务";
        }else if(2==type){
            body = "发布便民信息";
            detail = "发布便民信息";
        }else if(3==type){
            body = "购买同城促销商品";
            detail = "购买同城促销商品";
        }else if(4==type){
            body = "发布全民推广";
            detail = "发布全民推广";
        }else if(5==type){
            body = "购买大商惠购商品";
            detail = "购买大商惠购商品";
        }else if(6==type){
            body = "购买vip会员";
            detail = "购买vip会员";
        }

		Map<String, Object> resMap = WxXcxPayService.createXcxPrepay(openId, ip, orderNo, money, body, detail);

		return resMap;
	}

	public TAbbOrder getOrderFormByOrderNo(String orderNo){
		TAbbOrder order = tAbbOrderMapper.getOrderFormByOrderNo(orderNo);
		return order;
	}

	@Transactional
	public void handlePayInfo(TAbbOrder order){
		DecimalFormat df = new DecimalFormat("0.00");
	    int type = order.getType();
	    if(1==type){//发布全城任务
	    	double balance = 0d;

			//计算需要分给平台的金额
			TAbbQczifei qczifei = qczifeiMapper.SelectLimitone();
			double ptChoucheng = order.getPaymoney()*(Double.parseDouble(qczifei.getPingtaiscore())/100);
			TAbbCount count = countMapper.selectLimitone();
			count.setCount(Double.parseDouble(df.format(count.getCount()+ptChoucheng)));
			countMapper.updateByPrimaryKey(count);
			//增加一条平台的收益记录
			TAbbCommission commission = new TAbbCommission();
			commission.setId(SnowflakeIdWorker.getUUID());
			commission.setCreattime(DateUtil.getCurrentDateTimeStr());
			commission.setType(3);
			commission.setMemberid(order.getUserid());
			commission.setMessage("任务提成");
			commission.setBeiyongone(df.format(ptChoucheng));
			commission.setBeiyongtwo(order.getObjid());
			commissionMapper.insert(commission);

			//剩余的任务金
			balance = order.getPaymoney()-ptChoucheng;

	    	//修改全城任务支付状态
	    	TAbbQuancheng quancheng = quanchengMapper.selectByPrimaryKey(order.getObjid());
	    	quancheng.setPaystatus("2");
	    	quancheng.setBalance(Double.parseDouble(df.format(balance)));
	    	quanchengMapper.updateByPrimaryKey(quancheng);

	    }else if(2==type){//发布便民信息
	    	//修改便民信息的支付状态
	    	TAbbBiammin biammin = biamminMapper.selectByPrimaryKey(order.getObjid());
	    	biammin.setYuliu3("1");
	    	biamminMapper.updateByPrimaryKey(biammin);

	    	//便民信息的任务费和置顶费加入平台账户
			double pingtaiMoney = order.getGoodsmoney()+order.getRenwumoney();
			TAbbCount count = countMapper.selectLimitone();
			count.setCount(Double.parseDouble(df.format(count.getCount()+pingtaiMoney)));
			countMapper.updateByPrimaryKey(count);


			//减去会员的置顶次数
			TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(order.getUserid());
			int zhidingnum = biammin.getDays()-order.getGoodsmoney().intValue();
			member.setZhidingnum(member.getZhidingnum()-zhidingnum);
			tAbbMemberMapper.updateByPrimaryKey(member);

			if(pingtaiMoney>0){
				//增加一条平台的收益记录
				TAbbCommission commission = new TAbbCommission();
				commission.setId(SnowflakeIdWorker.getUUID());
				commission.setCreattime(DateUtil.getCurrentDateTimeStr());
				commission.setType(3);
				commission.setMemberid(order.getUserid());
				commission.setMessage("任务提成");
				commission.setBeiyongone(df.format(pingtaiMoney));
				commission.setBeiyongtwo(order.getObjid());
				commissionMapper.insert(commission);
			}

	    }else if(3==type){//购买同城促销商品

			TAbbCxzifei cxzifei = cxzifeiMapper.selectLimitone();

			TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(order.getUserid());
			String yqrid = member.getYqrid();
			TAbbMember yqr = tAbbMemberMapper.selectByPrimaryKey(yqrid);
			if(null!=yqrid&&!"".equals(yqrid)&&yqr.getIsvip()==1){
				double yqMoney = order.getPaymoney()*(Double.parseDouble(cxzifei.getShangjiscore())/100);
				yqr.setAccount(Double.parseDouble(df.format(yqr.getAccount()+yqMoney)));
				tAbbMemberMapper.updateByPrimaryKey(yqr);

				//增加一条上级的收益记录
				TAbbCommission commission = new TAbbCommission();
				commission.setId(SnowflakeIdWorker.getUUID());
				commission.setCreattime(DateUtil.getCurrentDateTimeStr());
				commission.setMemberid(yqrid);
				commission.setBusinessid("1");
				commission.setType(1);
				commission.setMessage("促销商品抽成");
				commission.setBeiyongone(df.format(yqMoney));
				commission.setBeiyongtwo(order.getObjid());
				commissionMapper.insert(commission);

				//增加一条平台收益记录
				double ptMoney = order.getPaymoney()*(Double.parseDouble(cxzifei.getPingtaiscore())/100);
				TAbbCount count = countMapper.selectLimitone();
				count.setCount(Double.parseDouble(df.format(count.getCount()+ptMoney)));
				countMapper.updateByPrimaryKey(count);
				//增加一条平台的收益记录
				TAbbCommission commissionPt = new TAbbCommission();
				commissionPt.setId(SnowflakeIdWorker.getUUID());
				commissionPt.setCreattime(DateUtil.getCurrentDateTimeStr());
				commissionPt.setType(3);
				commission.setMemberid(order.getUserid());
				commissionPt.setBusinessid("2");
				commissionPt.setMessage("促销商品收益");
				commissionPt.setBeiyongone(df.format(ptMoney));
				commissionPt.setBeiyongtwo(order.getObjid());
				commissionMapper.insert(commissionPt);

			}else{//没有上级所有收益给平台
				double score = Double.parseDouble(cxzifei.getShangjiscore())+Double.parseDouble(cxzifei.getPingtaiscore());
				double ptMoney = order.getPaymoney()*(score/100);
				TAbbCount count = countMapper.selectLimitone();
				count.setCount(Double.parseDouble(df.format(count.getCount()+ptMoney)));
				countMapper.updateByPrimaryKey(count);
				//增加一条平台的收益记录
				TAbbCommission commission = new TAbbCommission();
				commission.setId(SnowflakeIdWorker.getUUID());
				commission.setCreattime(DateUtil.getCurrentDateTimeStr());
				commission.setBusinessid("2");
				commission.setType(3);
				commission.setMemberid(order.getUserid());
				commission.setMessage("促销商品收益");
				commission.setBeiyongone(df.format(ptMoney));
				commission.setBeiyongtwo(order.getObjid());
				commissionMapper.insert(commission);
			}


	    }else if(4==type){//发布全民推广信息
	    	TAbbQuanmin quanmin = quanminMapper.selectByPrimaryKey(order.getObjid());
	    	quanmin.setYuliu2("1");
	    	quanminMapper.updateByPrimaryKey(quanmin);

	    	//计算分给平台的金额
			TAbbTgzifei tAbbTgzifei = tAbbTgzifeiMapper.selectTgzifei();
			double ptChoucheng = order.getPaymoney()*(Double.parseDouble(tAbbTgzifei.getPingtaiscore())/100);
			TAbbCount count = countMapper.selectLimitone();
			count.setCount(Double.parseDouble(df.format(count.getCount()+ptChoucheng)));
			countMapper.updateByPrimaryKey(count);
			//增加一条平台的收益记录
			TAbbCommission commission = new TAbbCommission();
			commission.setId(SnowflakeIdWorker.getUUID());
			commission.setCreattime(DateUtil.getCurrentDateTimeStr());
			commission.setType(3);
			commission.setMemberid(order.getUserid());
			commission.setBusinessid("1");
			commission.setMessage("任务提成");
			commission.setBeiyongone(df.format(ptChoucheng));
			commission.setBeiyongtwo(order.getObjid());
			commissionMapper.insert(commission);

			//计算分给发布人上级的佣金,如果没有上级给平台
			TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(order.getUserid());
			String yqrid = member.getYqrid();
			TAbbMember yqr = tAbbMemberMapper.selectByPrimaryKey(yqrid);
			if(null!=yqrid&&!"".equals(yqrid)&&yqr.getIsvip()==1){//有上级给上级分
				double yqMoney = order.getPaymoney()*(Double.parseDouble(tAbbTgzifei.getShangjiscore())/100);
				yqr.setAccount(Double.parseDouble(df.format(yqr.getAccount()+yqMoney)));
				tAbbMemberMapper.updateByPrimaryKey(yqr);

				//增加一条上级的收益记录
				TAbbCommission commissionSj = new TAbbCommission();
				commissionSj.setId(SnowflakeIdWorker.getUUID());
				commissionSj.setCreattime(DateUtil.getCurrentDateTimeStr());
				commissionSj.setMemberid(yqrid);
				commissionSj.setBusinessid("1");
				commissionSj.setType(1);
				commissionSj.setMessage("任务提成");
				commissionSj.setBeiyongone(df.format(yqMoney));
				commissionSj.setBeiyongtwo(order.getObjid());
				commissionMapper.insert(commissionSj);
			}else{//没有上级分给平台
				double yqMoney = order.getPaymoney()*(Double.parseDouble(tAbbTgzifei.getShangjiscore())/100);
				count.setCount(Double.parseDouble(df.format(count.getCount()+yqMoney)));
				countMapper.updateByPrimaryKey(count);
				//增加一条平台的收益记录
				TAbbCommission commissionPt = new TAbbCommission();
				commissionPt.setId(SnowflakeIdWorker.getUUID());
				commissionPt.setCreattime(DateUtil.getCurrentDateTimeStr());
				commissionPt.setType(3);
				commission.setMemberid(order.getUserid());
				commissionPt.setBusinessid("1");
				commissionPt.setMessage("任务提成");
				commissionPt.setBeiyongone(df.format(yqMoney));
				commissionPt.setBeiyongtwo(order.getObjid());
				commissionMapper.insert(commissionPt);
			}

	    }else if(5==type){//购买大商惠购商品
	    	order.setYuliu1("1");
			String xiaofeima = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
	    	order.setYuliu2(xiaofeima);

	    }else if(6==type){//购买vip会员
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //显示日期
			Calendar calender = Calendar.getInstance();//当前时间

			TAbbTaocan tAbbTaocan = tAbbTaocanMapper.getVipTaocan().get(0);
	    	TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(order.getUserid());
			try {
				if(0==member.getIsvip()){
					member.setIsvip(1);
					calender.add(Calendar.YEAR, +1);//vip到期时间
					String timeEnd = sdf.format(calender.getTime());
					member.setVipendtime(sdf.parse(timeEnd));
				}else{
					Date vipEndTime = member.getVipendtime();
					long endTime = vipEndTime.getTime();
					if(endTime<new Date().getTime()){//vip已过期
						calender.add(Calendar.YEAR, +1);
						String timeEnd = sdf.format(calender.getTime());
						member.setVipendtime(sdf.parse(timeEnd));
					}else{//vip未过期，续费
						calender.setTime(vipEndTime);
						calender.add(Calendar.YEAR, +1);
						String timeEnd = sdf.format(calender.getTime());
						//设置vip的到期时间
						member.setVipendtime(sdf.parse(timeEnd));
					}
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}

			//给用户加上置顶次数
			member.setZhidingnum(member.getZhidingnum()+Integer.parseInt(tAbbTaocan.getZhiding()));
			member.setYuliu2(DateUtil.getCurrentDateTimeStr());
			tAbbMemberMapper.updateByPrimaryKey(member);

			String yqrid = member.getYqrid();
			TAbbMember yqr = tAbbMemberMapper.selectByPrimaryKey(yqrid);
			if(null!=yqrid&&!"".equals(yqrid)&&yqr.getIsvip()==1){
				double bili = tAbbTaocan.getScore()/100d;
				double yqMoney = order.getPaymoney()*bili;
				yqr.setAccount(Double.parseDouble(df.format(yqr.getAccount()+yqMoney)));
				tAbbMemberMapper.updateByPrimaryKey(yqr);

				//增加一条上级的收益记录
				TAbbCommission commission = new TAbbCommission();
				commission.setId(SnowflakeIdWorker.getUUID());
				commission.setCreattime(DateUtil.getCurrentDateTimeStr());
				commission.setMemberid(yqrid);
				commission.setBusinessid("1");
				commission.setType(1);
				commission.setMessage("会员抽成");
				commission.setBeiyongone(df.format(yqMoney));
				commission.setBeiyongtwo(order.getObjid());
				commissionMapper.insert(commission);

				//剩余金额进入平台账户
				double leftMoney = order.getPaymoney()-yqMoney;
				TAbbCount count = countMapper.selectLimitone();
				count.setCount(Double.parseDouble(df.format(count.getCount()+leftMoney)));
				countMapper.updateByPrimaryKey(count);
				//增加一条平台的收益记录
				TAbbCommission commissionPt = new TAbbCommission();
				commissionPt.setId(SnowflakeIdWorker.getUUID());
				commissionPt.setCreattime(DateUtil.getCurrentDateTimeStr());
				commissionPt.setType(3);
				commission.setMemberid(order.getUserid());
				commissionPt.setBusinessid("1");
				commissionPt.setMessage("会员抽成");
				commissionPt.setBeiyongone(df.format(leftMoney));
				commissionPt.setBeiyongtwo(order.getObjid());
				commissionMapper.insert(commissionPt);

			}else{//没有上级所有钱进入平台账户
				TAbbCount count = countMapper.selectLimitone();
				count.setCount(Double.parseDouble(df.format(count.getCount()+order.getPaymoney())));
				countMapper.updateByPrimaryKey(count);
				//增加一条平台的收益记录
				TAbbCommission commissionPt = new TAbbCommission();
				commissionPt.setId(SnowflakeIdWorker.getUUID());
				commissionPt.setCreattime(DateUtil.getCurrentDateTimeStr());
				commissionPt.setType(3);
				commissionPt.setMemberid(order.getUserid());
				commissionPt.setBusinessid("1");
				commissionPt.setMessage("会员抽成");
				commissionPt.setBeiyongone(String.valueOf(order.getPaymoney()));
				commissionPt.setBeiyongtwo(order.getObjid());
				commissionMapper.insert(commissionPt);
			}
	    }

	    order.setPaystatus(1);
	    order.setPaytime(DateUtil.getCurrentDateTimeStr());
	    tAbbOrderMapper.updateByPrimaryKey(order);
	}

	public Map<String,Object> getOrderDetail(String orderid){
		Map<String,Object> resMap = new HashMap<>();
		TAbbOrder order = tAbbOrderMapper.selectByPrimaryKey(orderid);
		TAbbGoods goods = tAbbGoodsMapper.selectByPrimaryKey(order.getObjid());
		int type = order.getType();
		if(5==type){
            TAbbRenzheng renzheng = tAbbRenzhengMapper.selectByPrimaryKey(goods.getShopid());
            resMap.put("shop",renzheng);
		}
		resMap.put("order",order);
		resMap.put("goods",goods);
		resMap.put("success",true);
		resMap.put("msg","获取订单详情成功");
		return resMap;
	}

	public PageInfo<TAbbOrder> memberxiaofei(Tablepar tablepar, String searchTxt, String starttime, String endtime) {
		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		TAbbOrderExample testExample=new TAbbOrderExample();
		TAbbOrderExample.Criteria criteria=testExample.createCriteria();
		if (!StringUtils.isBlank(searchTxt)){
			criteria.andYuliu4Like("%"+searchTxt+"%");
		}
		if (!StringUtils.isBlank(starttime)){
			criteria.andCreatetimeGreaterThanOrEqualTo(starttime);
		}
		if(!StringUtils.isBlank(endtime)){
			criteria.andCreatetimeLessThanOrEqualTo(endtime);
		}
		criteria.andPaystatusGreaterThan(0);
		List<TAbbOrder> list= tAbbOrderMapper.selectByExample(testExample);
		PageInfo<TAbbOrder> pageInfo = new PageInfo<TAbbOrder>(list);
		return pageInfo;
	}
}
